Person.h定義Person類對象,以及Person類的比較,賦值,和輸出重載
#pragma once
#include<string>
#include<ostream>
using namespace std;
class Person
{
friend ostream &operator<<(ostream &out, Person &person); // 輸出運算符重載
public:
string name;
string phone;
Person & operator=(Person &person); // 賦值運算符重載
bool operator==(Person &person); // 判斷相等運算符重載
};
Person.cpp實現了Person.h中的重載運算符函數
#include "Person.h"
ostream &operator<<(ostream &out, Person &person)
{
out << person.name <<" "<< person.phone<<endl;
return out;
}
Person & Person::operator=(Person &person) // 賦值運算符重載
{
this->name = person.name;
this->phone = person.phone;
return *this;
}
bool Person::operator==(Person &person)
{
if (this->name == person.name)
{
return true;
}
return false;
}
Node.h定義了節點類
#pragma once
#include "Person.h"
class Node
{
public:
Person data;
Node *next;
void printNode();
};
Node.cpp實現成員函數
#include "Node.h"
#include<iostream>
void Node::printNode()
{
std::cout << data << std::endl;
}
List.h定義了鏈表的頭指針和操作函數
#pragma once
#include"Node.h"
#include<fstream>
using namespace std;
class List
{
private:
Node * m_pList;
int m_iLength;
public:
List();
~List();
void ClearList(); // 清空整個鏈表
bool ListEmpty(); // 判斷鏈表是否爲空
bool ListInsertHead(Node *pNode); // 頭部插入一個節點
bool ListInsertTail(Node *pNode); // 尾部插入一個節點
bool ListInsert(int i, Node *pNode); // 從任意位置插入節點
bool ListDelete(int i, Node *pNode); // 從任意位置刪除結點
bool ListGetElem(int i, Node *pNode); // 查找第i個元素
int LocateElem(Node *pNode); // 查找結點的位序
bool PriorElem(Node *pCurrentNode, Node *pPreNode); // 找指針的前驅指針
bool NextElem(Node *pCurrentNode, Node *pNextNode); // 找指針的後繼指針
void ListTraverse(); // 鏈表的遍歷
void ListSaveData(char *file); // 將鏈表數據保存到文件中
};
List.cpp實現了鏈表的操作函數
#include "List.h"
List::List()
{
m_pList = new Node;
// m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
else
{
return false;
}
}
void List::ClearList()
{
Node *currentNode = m_pList->next; // 指向頭指針後第一個元素
while (currentNode != NULL)
{
Node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
m_pList->next = NULL; // 頭指針下一個爲空
}
List::~List()
{
ClearList();
delete m_pList;
m_pList = NULL;
}
bool List::ListInsertHead(Node *pNode)
{
Node *temp = m_pList->next; // 頭節點後第一個元素
Node *newNode = new Node; // 創建一個新的節點
if (newNode == NULL)
return false;
newNode->data = pNode->data;
m_pList->next = newNode;
newNode->next = temp;
m_iLength++;
return true;
}
bool List::ListInsertTail(Node *pNode)
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
currentNode = currentNode->next; // 遍歷到尾結點
Node *newNode = new Node;
if (newNode == NULL)
return false;
newNode->data = pNode->data;
newNode->next = NULL;
currentNode->next = newNode;
m_iLength++;
return true;
}
bool List::ListInsert(int i, Node *pNode)
{
if (i<0 || i>m_iLength)
return false;
Node *currentNode = m_pList;
for (int k = 0; k < i; k++)
{
currentNode = currentNode->next;
}
Node *newNode = new Node;
if (newNode == NULL)
return false;
newNode->data = pNode->data;
newNode->next = currentNode->next;
currentNode->next = newNode;
m_iLength++;
return true;
}
bool List::ListDelete(int i, Node *pNode)
{
if (i < 0 || i >m_iLength)
return false;
Node *currentNode = m_pList;
Node *currentNodeBefore = NULL;
for (int k = 0; k <= i; k++)
{
currentNodeBefore = currentNode;
currentNode = currentNode->next;
}
currentNodeBefore->next = currentNode->next;
pNode->data = currentNode->data;
delete currentNode;
currentNode = NULL;
m_iLength--;
return true;
}
bool List::ListGetElem(int i, Node *pNode)
{
if (i < 0 || i >m_iLength)
return false;
Node *currentNode = m_pList;
for (int k = 0; k <= i; k++)
{
currentNode = currentNode->next;
}
pNode->data = currentNode->data;
return true;
}
int List::LocateElem(Node *pNode)
{
Node *currentNode = m_pList;
int count = 0;
while (currentNode->next!= NULL)
{
currentNode = currentNode->next;
if (currentNode->data == pNode->data)
{
return count;
}
count++;
}
return -1;
}
bool List::PriorElem(Node *pCurrentNode, Node *pPreNode)
{
Node *currentNode = m_pList;
Node *tempNode = NULL;
while (currentNode->next != NULL)
{
tempNode = currentNode;
currentNode = currentNode->next;
if (currentNode->data == pCurrentNode->data)
{
if (tempNode == m_pList)
return false;
else
{
pPreNode->data = tempNode->data;
return true;
}
}
}
return false;
}
bool List::NextElem(Node *pCurrentNode, Node *pNextNode)
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (currentNode->data == pCurrentNode->data)
{
if (currentNode->next == NULL)
{
return false;
}
else
{
pNextNode->data = currentNode->next->data;
return true;
}
}
}
return false;
}
void List::ListTraverse()
{
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
void List::ListSaveData(char *file)
{
ofstream destFile(file, ios::app);
Node *currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
destFile << currentNode->data;
}
destFile.close();
}
demo.cpp對list進行調用,實現了功能,文件的輸入輸出使用C++的<fstream>庫
#include "List.h"
#include<iostream>
#include<fstream>
using namespace std;
void ShowOrder();
void createPersonTail(List *pList);
void createPersonHead(List *pList);
void createPerson(List *pList);
void searchPerson(List *pList);
void deletePerson(List *pList);
void ReadStuFile(List *pList);
void SaveStuFile(List *pList);
//void ModifyPerson(List *pList);
int main()
{
// 顯示操作界面
int nOrder;
ShowOrder();
bool flag = true;
List *pList = new List();
// 讀取文件中學生信息
// ReadStuFile(pList);
while (flag)
{
printf("請輸入指令,輸入0查看指令:\n");
cin >> nOrder;
switch (nOrder)
{
case 1:
// 添加一個學生的信息(尾添加)
createPersonTail(pList);
break;
case 11:
// 添加一個學生的信息(頭添加)
createPersonHead(pList);
break;
case 111:
// 添加一個學生的信息(從指定位置)
createPerson(pList);
case 2:
// 根據學號查找學生信息
searchPerson(pList);
break;
case 3:
// 修改指定學生的信息
// 根據學號查找學生信息
// ModifyPerson(pList);
break;
case 4:
SaveStuFile(pList);
cout << "成功寫入文件" << endl;
break;
case 5:
// 從文件讀取數據到鏈表
ReadStuFile(pList);
cout << "加載數據庫成功" << endl;
break;
case 6:
// 根據學號,刪除指定的學生信息
deletePerson(pList);
break;
case 7:
// 從文件恢復刪除的學生的信息
pList->ClearList();
ReadStuFile(pList);
break;
case 8:
// 顯示鏈表內容
pList->ListTraverse();
break;
case 9:
// 退出程序
flag = false;
break;
case 0:
// 顯示指令
ShowOrder();
break;
default:
printf("輸入指令不正確\n");
break;
}
}
// 釋放鏈表
pList->ClearList();
return 0;
}
void ShowOrder()
{
printf("**********************通訊錄管理系統********************\n");
printf("********************本系統操作指令如下******************\n");
printf("*** 1. 增加一個好友信息(從表尾) ***\n");
printf("*** 11. 增加一個好友信息(從表頭) ***\n");
printf("*** 111. 增加一個好友信息(從指定位置) ***\n");
printf("*** 2. 查找指定好友的信息(姓名/學號) ***\n");
printf("*** 3. 修改指定好友的信息 ***\n");
printf("*** 4. 保存業主的信息到文件中 ***\n");
printf("*** 5. 讀取文件中的好友的信息 ***\n");
printf("*** 6. 刪除指定好友的信息 ***\n");
printf("*** 7. 恢復刪除的好友的信息 ***\n");
printf("*** 8. 顯示所有的好友的信息 ***\n");
printf("*** 9. 退出系統 ***\n");
printf("********************************************************\n");
}
void createPersonTail(List *pList)
{
Node node;
Person person;
cout << "請輸入姓名:";
cin >> person.name;
cout << "請輸入電話:";
cin >> person.phone;
node.data = person;
if (pList->ListInsertTail(&node))
cout << "添加成功" << endl;
else
cout << "添加失敗" << endl;
}
void createPersonHead(List *pList)
{
Node node;
Person person;
cout << "請輸入姓名:";
cin >> person.name;
cout << "請輸入電話:";
cin >> person.phone;
node.data = person;
if (pList->ListInsertTail(&node))
cout << "添加成功" << endl;
else
cout << "添加失敗" << endl;
}
void createPerson(List *pList)
{
Node node;
Person person;
int i;
cout << "請輸入姓名:";
cin >> person.name;
cout << "請輸入電話:";
cin >> person.phone;
cout << "請輸入添加的位置:";
cin >> i;
node.data = person;
if (pList->ListInsert(i,&node))
cout << "添加成功" << endl;
else
cout << "添加失敗" << endl;
}
void searchPerson(List *pList)
{
Node node, ans;
Person person;
int i;
cout << "請輸入要查找好友的姓名:";
cin >> person.name;
person.phone = "";
node.data = person;
i = pList->LocateElem(&node);
if (pList->ListGetElem(i, &ans))
ans.printNode();
else
printf("沒有該好友信息\n");
}
void deletePerson(List *pList)
{
Node node;
Person person;
int i;
cout << "請輸入要刪除好友的姓名";
cin >> person.name;
person.phone = "";
node.data = person;
i = pList->LocateElem(&node);
if (pList->ListDelete(i, &node))
cout << "刪除好友" << person.name << "成功" << endl;
else
cout << "刪除好友" << person.name << "失敗" << endl;
}
void ReadStuFile(List *pList)
{
ifstream srcFile("AddressList.txt", ios::in);
Person person;
Node node;
while (srcFile >> person.name >> person.phone)
{
node.data = person;
pList->ListInsertTail(&node);
}
srcFile.close();
}
void SaveStuFile(List *pList)
{
char file[30] = "AddressList.txt";
cout << file << endl;
pList->ListSaveData(file);
}